import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Shapes 1.15

SubView0{
    id: root
    width: 252
    height: 325
    //property bool showLoader: false
    Component.onCompleted: {
        mymodbus.showAccumulateWeightSum(false)
    }
    Column
    {
        x:60
        y:15
        spacing: 5

        Button
        {
            id:btnMode
            width: 136
            height: 50
            background: Rectangle{
                border.width: 0
                color: "#7584C2"
                    }

            text: mymodbus.currentMode===0 ? qsTr("正常模式") : qsTr("累加模式")
            font.pixelSize: 25
            palette.buttonText: "white"
            onClicked: {
                //mainLoader.running = true;
                //mainLoader.visible = true;
                if(btnMode.text === qsTr("正常模式"))
                {
                    mymodbus.setCurrentMode(1);
                    btnMode.text = qsTr("累加模式");
                }
                else
                {
                    mymodbus.setCurrentMode(0);
                    btnMode.text = qsTr("正常模式");
                }
                recShow.border.width = 0
            }
        }
        Button
        {
            id:btnAccumulateMode
            anchors.horizontalCenter: parent.horizontalCenter
            //visible: btnMode.text === qsTr("累加模式")
            width: 100
            height: 40
            background: Rectangle{
                border.width: 0
                color: btnMode.text === qsTr("累加模式")?"#40A0A0":"gray"
                    }
enabled: btnMode.text === qsTr("累加模式")
            text: mymodbus.accumulateMode === 0 ? qsTr("手动") : qsTr("自动")
            font.pixelSize: 25
            palette.buttonText: "white"
            onClicked: {
                //mainLoader.running = true;
                //mainLoader.visible = true;
                if(btnAccumulateMode.text === qsTr("手动"))
                {
                    mymodbus.setAccumulateMode(1)
                    btnAccumulateMode.text = qsTr("自动");
                }
                else
                {
                    mymodbus.setAccumulateMode(0)
                    btnAccumulateMode.text = qsTr("手动");
                }
            }
        }
        Button
        {
            id:btnManualAdd
            anchors.horizontalCenter: parent.horizontalCenter
            //visible: btnMode.text === qsTr("累加模式") && btnAccumulateMode.text === qsTr("手动")
            width: 100
            height: 40
            background: Rectangle{
                border.width: 0
                color:(btnMode.text === qsTr("累加模式") && btnAccumulateMode.text === qsTr("手动"))? "#40A0A0":"gray"
                    }
enabled:(btnMode.text === qsTr("累加模式") && btnAccumulateMode.text === qsTr("手动"))
            text: "M+"
            font.pixelSize: 25
            palette.buttonText: "white"
            onClicked: {
                mymodbus.appendAccumulateWeights(mymodbus.weightNew)
            }
        }
        Button
        {
            id:btnShow
            anchors.horizontalCenter: parent.horizontalCenter
            //visible: btnMode.text === qsTr("累加模式")
            width: 100
            height: 40
            background: Rectangle{
                id:recShow
                border.width: 0
                border.color: "#F0D070"
                color: btnMode.text === qsTr("累加模式")?"#40A0A0":"gray"
                    }
enabled: btnMode.text === qsTr("累加模式")
            text: "="
            font.pixelSize: 25
            palette.buttonText: "white"
            onClicked: {
                if(recShow.border.width > 0)
                {
                    mymodbus.showAccumulateWeightSum(false)
                    recShow.border.width = 0
                }
                else
                {
                    mymodbus.showAccumulateWeightSum(true)
                    recShow.border.width = 2
                }
            }
        }
        Button
        {
            id:btnClear
            anchors.horizontalCenter: parent.horizontalCenter
            //visible: btnMode.text === qsTr("累加模式")
            width: 100
            height: 40
            background: Rectangle{
                border.width: 0
                color: btnMode.text === qsTr("累加模式")?"#40A0A0":"gray"
                    }
enabled:btnMode.text === qsTr("累加模式")
            text: "MC"
            font.pixelSize: 25
            palette.buttonText: "white"
            onClicked: {
                mymodbus.clearAccumulateWeights();
            }
        }
        Button
        {
            id:btnSave
            width: 136
            height: 50
            background: Rectangle{
                border.width: 0
                color: "#7584C2"
                    }

            text: qsTr("保存数据")
            font.pixelSize: 25
            palette.buttonText: "white"
            onClicked: {
                //mainLoader.running = true;
                //mainLoader.visible = true;
                if(mymodbus.weightNew === "")
                {
                    saveWeightOKDialog.title=qsTr("无重量值！");
                    saveWeightOKDialog.open()
                    return;
                }

                if(mymodbus.saveCurrentWeight(mymodbus.weightNew, mymodbus.unit)){
                    saveWeightOKDialog.title=qsTr("保存成功！")
                }
                else{
                    saveWeightOKDialog.title=qsTr("保存失败！")
                }

                //mainLoader.running = false;
                //mainLoader.visible = false;
                saveWeightOKDialog.open()
            }
        }
    }


}

